/**加载脚本，触发回调函数,用法用下
 *require("test.js",funciton(){...});//加载 test.js
 *require("test.js","test2.js",funciton(){...});//加载 test.js test2.js 加载完毕test.js 后才加载test2.js 然后回调 
 *require(["test.js","test2.js"],funciton(){...});//同时加载 test.js test2.js 全部加载完毕后执行回调
 *require("test0.js",["test.js","test2.js"],funciton(){...});//先加载 test0.js 再同时加载 test.js test2.js 然后回调
 *vesion 1.0; 
 *time 2012.5.16,by Fyx in liettou Company
 */
(function(d,w){
	var stack_data_args=[],//参数和出栈函数
		stack_callBack=[], //回调函数
		stackFlag=1,
		charset="utf-8";
	
	///测试时间代码
	function time(){
		this.time=(new Date).getTime();
	}
	time.prototype={
		end:function(){
			return (new Date).getTime()-this.time;
		}
	}
	function loadJS(url,callback){
		var head=document.head||document.getElementsByTagName("head")[0],
			script,
			options,
			setttings;
		if(is(url,"Object")){
			options=url;
			url=undefined;
		}
	
		settings=options||{};
		url=url||settings.url;
		callback=callback||settings.callback;
		
		script=document.createElement("script");
		script.async=settings.async||false;
		script.type="text/javascript";
		script.charset=charset||settings.charset;
		if(!!settings.cache){
			url=url+(/\?/.test(url)?"&":"?")+"_="+(new Date).getTime();
		}
		script.src=url+(/\?/.test(url)?"&":"?")+"_="+(new Date).getTime();
		head.appendChild(script);
		if(callback){
			document.addEventListener?script.addEventListener("load",function(){
				setTimeout(function(){
						callback(url);
					},0);
				},false):
			script.onreadystatechange=function(){
				if(/load|complete/.test(script.readyState)){
					script.onreadystatechange=null;
					script=null;
					
					setTimeout(function(){
						callback(url);
					},0);
					
				}
			}
		}
	};
	//先进先出的方式出栈;
	function args_Shift(){  
		if(stack_data_args.length){
			alert(0);
			requreJs_type.apply(null,stack_data_args.shift());//此处可能出现错误，call是以字符传的的格式传值,而apply 则是以数组的格式的传值
		}else if(stack_callBack.length){
			alert(1);
			stack_callBack.pop()();
			arguments.callee();
		}else{
			alert(2);
			stackFlag=0; //初始化 stackFlag=0;
		}
	}
	//参数整理
	function args_push(url,callback){
		callback&&stack_callBack.push(callback);
		var i=0;
		// 这里的代码写法上可以尝试修改
		if(is(url,"String")){
			stack_data_args.push([url,args_Shift]);
		}else{
			for(;i<url.length;i++){
				stack_data_args.push([url[i],args_Shift]);
			}
		}
		if(stackFlag===0){
			stackFlag=1;
			args_Shift();
		}
	}
	function Requrejs(){
		var args=Array.prototype.slice.call(arguments);
			l=args.length;
		if(1===l){
			if(is(args[0],"String")){
				args_push(args[0]);
			}else{
				args_push(args);
			}
			return;
		}
		if(is(args[l-1],"Function")){
			var urls=Array.prototype.slice.call(args,0,l-1);
				callBack=args[l-1];
				args_push(urls,callBack);
			return;
		}
		stack_push(args);
	}
	function is(obj,type){
		return (obj===null&&type==='Null')||
			   (obj===undefined&&type==='Undefined')||
			   (type==='Function'&&"object"===typeof document.getElementById?
				/^\s*\bfunction\b/.test(""+obj):Object.prototype.toString.call(obj).indexOf(type)>-1);
	}
	/**DomContentLoaded事件
	 *页面contentLoaded完毕就激发此事件
	 */
	function domReady(){
		stackFlag=0;
		args_Shift();
	};
	//等loadjs这个文件代码加载完毕->解释到这的的时候加一个DOMContentLoaded事件,ie是onreadystatechange事件,去执行domReady()函数
	(function(d){
		d.addEventListener?d.addEventListener("DOMContentLoaded",function(){
			d.removeEventListener("DOMContentLoaded",arguments.callee,false);
				domReady();
			},false):d.attachEvent("onreadystatechange",function(){
			   if(/load|complete/.test(d.readyState)){
					d.detachEvent("onreadystatechange",arguments.callee);
					domReady();
			   }
			})
	})(d);
	function requreJs_type(url,callback){
		if(is(url,'String')){
			loadJS(url,callback);
			return;
		}
		var temp={};
		function loadBack(str){
			//str=str.replace(/[\/\;\:]/g,"").replace(/\./g,"-");
			delete temp[str];
			if(str in temp){
				return;
			}
			callback && callback();
		}
		for(var i=0;i<url.length;i++){
			temp[url[i]] = true;
		    loadJS(url[i],loadBack);
		}
	}	 
	w["Requrejs"]=Requrejs;
})(document,window);
